﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MailMergeToolForm.Helper;
using MailMergeToolForm.MailMergeService;

namespace MailMergeToolForm.MenuForm
{
    public partial class frMultiFileMailMerge : Form
    {
        private string[] listofFilePath; 
        public frMultiFileMailMerge()
        {
            InitializeComponent();
        }

        private void btn_ChoosePathSource_Click(object sender, EventArgs e)
        {
            if (folderBrowserSource.ShowDialog() == DialogResult.OK)
            {
                txtPathSource.Text = folderBrowserSource.SelectedPath;
              
            }
        }

        private void btn_ChoosePathDestination_Click(object sender, EventArgs e)
        {
            if (folderBrowserDestination.ShowDialog() == DialogResult.OK)
            {
                txtPathDestination.Text = folderBrowserDestination.SelectedPath;
            }
        }

        private void btn_Execute_Click(object sender, EventArgs e)
        {
            if(String.IsNullOrEmpty(txtPathDestination.Text))
            {
                MessageBox.Show("Điền đường dẫn output");
                return;
            }
            if (String.IsNullOrEmpty(txtPathSource.Text))
            {
                MessageBox.Show("Điền đường dẫn gốc");
                return;
            }
            if (String.IsNullOrEmpty(txtDestionation.Text))
            {
                MessageBox.Show("Điền giá trị replace");
                return;
            }
            if (String.IsNullOrEmpty(txtSource.Text))
            {
                MessageBox.Show("Điền giá trị cần replace");
                return;
            }
            listofFilePath = FileFolderHelper.GetAllFileWord(txtPathSource.Text);
            ServiceClient myServiceMailMegre = new ServiceClient();
            myServiceMailMegre.ReplaceMailMergeMultiFile(txtSource.Text, txtDestionation.Text, listofFilePath,txtPathDestination.Text);

          
        }

        private void btn_Replace_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(txtPathDestination.Text))
            {
                MessageBox.Show("Điền đường dẫn output");
                return;
            }
            if (String.IsNullOrEmpty(txtPathSource.Text))
            {
                MessageBox.Show("Điền đường dẫn gốc");
                return;
            }
            if (String.IsNullOrEmpty(txtDestionation.Text))
            {
                MessageBox.Show("Điền giá trị replace");
                return;
            }
            if (String.IsNullOrEmpty(txtSource.Text))
            {
                MessageBox.Show("Điền giá trị cần replace");
                return;
            }
           ServiceClient myServiceMailMegre = new ServiceClient();
            string[] listofFilePathReplace = FileFolderHelper.GetAllFileWord(txtPathDestination.Text + @"\OutPut");
            myServiceMailMegre.ReplaceMailMergeWithValue(txtSource.Text,txtDestionation.Text, listofFilePathReplace, txtPathDestination.Text);
        }
    }
}
